from datetime import date, datetime
from google.appengine.api.urlfetch import fetch
from horseanalysis.lib.hkjc_fetcher.fetcher_base import FetcherBase
from horseanalysis.model.quinella import Quinella
from horseanalysis.model.race import Race
import re

class InvestmentFetcher(FetcherBase):
  def getUrl(self):
    return "http://bet.hkjc.com/racing/getXML.aspx?type=pooltot&date=%(date_query)s&venue=%(venue)s&raceno=%(race_no)d" % \
      {'date_query': self.date_query, 'venue': self.venue, 'race_no': self.race_no}
  
  def getData(self):
    data = fetch(self.getUrl())
    self.batch_time.update_investment(data.content)
    return data.content
    
  def getResult(self):
    data = self.getData()
    pool = {'WIN': -1.0, 'PLA': -1.0, 'QIN': -1.0, 'QPL': -1.0, 'DBL': -1.0, 'TCE': -1.0, 'TRI':-1.0, 'F-F':-1.0}
    for result in re.finditer(r"<INV POOL=\"([A-Z-]*)\" TIME=\"([0-9.]*)\">([0-9.]*)</INV>", data):
      pool[result.group(1)] = float(result.group(3))
      
    self.batch_time.update_pool(pool['WIN'], pool['PLA'], pool['QIN'], pool['QPL'], pool['DBL'], pool['TCE'], pool['TRI'], pool['F-F'])